from bs4 import BeautifulSoup  #网页解析，获取数据
import re   #正则表达式，文字匹配                               
import urllib.request, urllib.error #定制URL，获取网页数据
import sqlite3 #数据库操作



def main():
    baseurl = "https://movie.douban.com/top250?start="
    datalist = getData(baseurl)
    savepath = ".\\doubanData.xls"
    saveData(savepath)
    askURL(baseurl)



def getData(baseurl):
    datalist = []
    # for i in range(0, 10):
    #     # url = 
    # # return datalist

def saveData(savepath):
    print("save")


def askURL(url):
    # 模拟浏览器头部信息
    head = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
    } 
    # 用户代理，表示告诉豆瓣服务器，我们是什么机器

    request = urllib.request.Request(url, headers=head)
    html = ""
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode("utf-8")
        print(html)
    except urllib.error.URLError as e:
        if hasattr(e, "code"):
            print(e.code)
        if hasattr(e, "reson"):
            print(e.reson)

    return html

if __name__ == '__main__':
    main()
    